//
//  LGBadgeView.m
//  微博
//
//  Created by MS on 15-12-3.
//  Copyright (c) 2015年 MS. All rights reserved.
//

#import "LGBadgeView.h"
#import "UIView+Frame.h"
#define LGBadgeViewFont [UIFont systemFontOfSize:11]

@implementation LGBadgeView

-(instancetype) initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.userInteractionEnabled = NO;
        
        [self setBackgroundImage:[UIImage imageNamed:@"main_badge"] forState:UIControlStateNormal];
        
        self.titleLabel.font =LGBadgeViewFont;
        
        [self sizeToFit];
        
    }
    
    return  self;
    
}


-(void)setBadgeValue:(NSString *)badgeValue
{
    
    _badgeValue = badgeValue;
    
    if (badgeValue.length==0 ||[badgeValue isEqualToString:@"0"]) {
        self.hidden =YES;
    }
    else{
        
        self.hidden = NO;
    }
    
   // CGSize size = [badgeValue sizeWithFont:LGBadgeViewFont];
    CGSize size = [badgeValue sizeWithAttributes:@{NSFontAttributeName:LGBadgeViewFont}];
    
    if (size.width >self.width) {
        [self setImage:[UIImage imageNamed:@"new_dot"] forState:UIControlStateNormal];
        [self setTitle:nil forState:UIControlStateNormal];
        [self setBackgroundImage:nil forState:UIControlStateNormal];
    }else{
        
        [self setBackgroundImage:[UIImage imageNamed:@"main_badge"] forState:UIControlStateNormal];
       [self setTitle:badgeValue forState:UIControlStateNormal];
        
        [self setImage:nil forState:UIControlStateNormal];
    }
    
    
    
}

@end
